package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("travels")
public class TravelController {



    @Autowired
    private ITravelService travelService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Autowired
    private ITravelCommentService travelCommentService;


    @GetMapping("/query")
    public Object query(TravelQuery qo){
        return JsonResult.success(travelService.query(qo));
    }

    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }




    @GetMapping("/detail")
    public Object detail(String id){
       return JsonResult.success(travelService.get(id));
    }


    /**
     *
     * 存在问题:
     *    后端获取当前登录用户信息使用方法
     * public Object xxx(HttpServletRequest request){
     *      String token = request.getHeader("token");
     *      UserInfo userInfo = userInfoRedisService.getUserByToken(token);
     * }
     * 其他接口要获取当前登录用户对象都需要执行上面几个步骤,出行代码重复
     *
     * 思考: 能不能简化一下,简化后的代码
     * 简化后代码底层实现原理: 自定义springmvc参数解析器
     *  //简化后操作
     *  @GetMapping("/info")
     *  public Object info(UserInfo userInfo){
     *       return JsonResult.success(userInfo);
     *  }
     * 思考:
     *   说明:
     *      springmvc 所有的映射方法中形式参数 的值注入都是靠
     *        springmvc自带参数解析器实现
     *
     *   现在想要springmvc将当前登录用户对象注入,需要
     *   自定义参数解析器
     */
    @RequireLogin
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel, HttpServletRequest request){

        String token = request.getHeader("token");
        UserInfo userInfo = userInfoRedisService.getUserByToken(token);

        travel.setUserId(userInfo.getId());
        travelService.saveOrUpdate(travel);
        return JsonResult.success(travel.getId());
    }

    /**
     * 需求: 使用简化的方式获取当期登录用户信息
     *
     * 直接在请求映射方法中声明UserInfo 这个类型形式参数,
     *   springmvc就自动将当期登录用户对象注入
     */
    @GetMapping("/info")
    public Object info(@UserParam UserInfo userInfo){
        //userInfo
        return JsonResult.success(userInfo);
    }


    //如果要进行用户编辑, 映射方法接收参数也是UserInfo, 此时怎么区分
    //定义一个参数级别注解, 当参数贴有这个注解, 自定义解析器才执行解析逻辑
    @GetMapping("/updateInfo")
    public Object updateInfo(UserInfo userInfo){
        //userInfo
        return JsonResult.success(userInfo);
    }

    @RequireLogin
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment comment, @UserParam UserInfo userInfo){


        BeanUtils.copyProperties(userInfo, comment);
        comment.setUserId(userInfo.getId());

        travelCommentService.save(comment);

        return JsonResult.success();
    }


    @GetMapping("/comments")
    public Object comments(String travelId){
        return JsonResult.success(travelCommentService.queryByTravelId(travelId));
    }






}
